﻿using EnterpriseInformation.Common;
using HtmlAgilityPack;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace EnterpriseInformation.DataModel
{
    public class CompanyBase : CustomLog
    {

        public CompanyBase()
        {
            InitLog();
            basedetail = new basedetail();
            //股东信息
            partnern = new Partnern();

            //主要负责人
            mainmember = new Mainmember();

            ////最终受益人
            beneficiary = new Beneficiary();

            ///工商登记  股东及出资信息 5
            stockholder = new Stockholder();

            ///工商登记  股权变更信息 11
            stockchange = new StockChange();

            //变更历史
            changelist = new Changelist();

            //登记信息
            registerInfo = new RegisterInfo();

            //全球关联企业
            overseaall = new Overseaall();
        }

        public basedetail basedetail { get; set; }
        public RegisterInfo registerInfo { get; set; }
        public Overseaall overseaall { get; set; }

        

        /// <summary>
        /// 股东信息
        /// </summary>
        public Partnern partnern { get; set; }
        
        /// <summary>
        /// 主要负责人
        /// </summary>
        public Mainmember mainmember { get; set; }
        /// <summary>
        /// 最终受益人
        /// </summary>
        public Beneficiary beneficiary { get; set; }
        /// <summary>
        /// 工商自主公示- 股东及出资信息 
        /// </summary>
        public Stockholder stockholder { get; set; }
        /// <summary>
        /// 股权变更信息
        /// </summary>
        public StockChange stockchange { get; set; }
        /// <summary>
        /// 财务分析
        /// </summary>
        public newFinancialAnalysis newfinancialanalysis { get; set; }
        /// <summary>
        /// 同业分析
        /// </summary>
        public newIndicatorData newindicatordata { get; set; }
        /// <summary>
        /// 变更记录
        /// </summary>
        public Changelist changelist { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <param name="CompanyId"></param>
        public void Init(string htmlContent, string CompanyId)
        {
            try
            {
                //https://www.cnblogs.com/browser/p/4455527.html
                //var document = new HtmlDocument { OptionAutoCloseOnEnd = true };
                //document.LoadHtml(item.content.Replace("\r", "").Replace("\n", ""));
                //HtmlNodeCollection panelBodys = document.DocumentNode.SelectNodes("//div[@class='law-title']");
                //HtmlNode panelBodys1 = document.DocumentNode.SelectSingleNode("//div[@id='law-title']");
                //doc.DocumentNode.SelectNodes("//section[@class=\"ntable ntable-list\"]/table/*/tr");
                ///选择不包含class属性的节点
                //var result = node.SelectNodes(".//span[not(@class)]");
                //选择不包含class和id属性的节点
                //var result = node.SelectNodes(".//span[not(@class) and not(@id)]");
                //选择不包含class="expire"的span
                //var result = node.SelectNodes(".//span[not(contains(@class,'expire'))]");
                //选择包含class="expire"的span
                //var result = node.SelectNodes(".//span[contains(@class,'expire')]");
                //通配符
                //可用* 表示任意节点名称，比如 SelectNodes("/channel/*/item")；
                //用 @*表示任意属性；
                //用 node() 表示任意类型的节点；
                //用 text() 表示文本类型的节点，实际上也就是表示取文本节点（元素节点下面是文本节点，文本节点下面才是文字）。
                //运算符
                //表示或。SelectNodes("//item|//channel")，选择 item 或 channel 节点。

                //所有元素 //*
                //所有的P元素 //p
                //所有的p元素的子元素  	//p/*
                //根据ID获取元素	//*[@id='foo']
                //根据Class获取元素     //*[contains(@class,'foo')] 1
                //拥有某个属性的元素 	//*[@title]
                //所有P元素的第一个子元素 	//p/*[0]
                //所有拥有子元素a的P元素   	//p[a]
                //class="foo"	//*[@class='foo']	class属性只有一个值foo
                //class="foobar foo bar"	//*[@class=' foo ']	class属性值中,foo在其他两边的值的中间
                //class="foo bar"//*[starts-with(@class,'foo ')]	class属性值中,foo在最左边
                //class="bar foo"	//*[substring(@class,string-length(@class)-3)=' foo']	class属性值中,foo在最右边,XPath1.0中没有ends-with函数,2.0有,现在浏览器实现的都是1.0

                //  1，  .//span  当前节点下的所有  //span 全局节点下的所有   span 当前节点的儿子节点（不包含儿子的儿子）   /span 当前节点平级节点
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(htmlContent);
                doc = CommonClass.HelperHtmlNode(doc);
                basedetail.CompanyId = CompanyId;
                basedetail.UpdateTime = doc.DocumentNode.SelectSingleNode("//p[@class=\"refs\"]")?.InnerText.Trim();
                basedetail.CompanyName = doc.DocumentNode.SelectSingleNode("//div[@id=\"company-top\"]//div[@class=\"content\"]/div[@class=\"row title jk-tip\"]/h1")?.InnerText.Trim();
                basedetail.CompanyIMG = doc.DocumentNode.SelectSingleNode("//div[@id=\"company-top\"]//div[@class=\"imgkuang\"]/img")?.Attributes["src"]?.Value.Trim();
                HtmlNodeCollection tagNodes =
                     doc.DocumentNode.SelectNodes("//div[@class=\"content\"]/div[contains(@class,\"row\") and contains(@class,\"tags\")]");
                HtmlNodeCollection childTagNodes = tagNodes?[0].SelectNodes("./*/text()|./*/*/text()");
                if (childTagNodes != null)
                {
                    foreach (HtmlNode childNoteItem in childTagNodes)
                    {
                        string tagText = childNoteItem.InnerText.Replace(" ", "").Replace("\n", "");
                        if (!string.IsNullOrEmpty(tagText) && !tagText.Contains("风险") && !tagText.Contains("条"))
                        {
                            basedetail.TagList.Add(tagText);
                        }
                    }
                }
                

                HtmlNodeCollection basicRows = doc.DocumentNode.SelectNodes("//div[@class=\"dcontent\"]/div[@class=\"row\"]");

                HtmlNodeCollection phoneNodes = basicRows?[0].SelectNodes("./span[contains(@class,\"fc\")]/span[contains(@class,\"cvlu\")]");

                var phoneNodeSpan = phoneNodes?[0].SelectNodes("./span/text()")?.Where(p => !string.IsNullOrEmpty(p.InnerText.Replace(" ", ""))).ToList();

                string phoneNum = string.Empty;

                if (phoneNodeSpan != null && phoneNodeSpan.Count > 0)
                {
                    phoneNum = phoneNodeSpan[0].InnerText;

                }
                else
                {

                    phoneNum = phoneNodes?[0].InnerText;
                }

                basedetail.PhoneNum = phoneNum?.Replace(" ", "").Trim();
                basedetail.WebSite = basicRows?[0].SelectSingleNode("./span[contains(@class,\"cvlu\")]/a")?.InnerText.Replace(" ", "").Trim();
                HtmlNodeCollection mailBoxNodes = basicRows?[1].SelectNodes("./span[contains(@class,\"fc\")]/span");
                HtmlNodeCollection mailBoxNode = mailBoxNodes?[1].SelectNodes("./a");
                string mailBox = string.Empty;
                if (mailBoxNode != null && mailBoxNode.Count > 0)
                {
                    mailBox = mailBoxNode[0].InnerText;

                }
                else
                {

                    mailBox = mailBoxNodes?[0].InnerText;
                }
                basedetail.MailBox = mailBox?.Replace(" ", "").Trim();


                basedetail.Address = basicRows?[1].SelectNodes("./span[contains(@class,\"cvlu\")]/a/text()")?[0].InnerText.Replace(" ", "");
                string intruduce = string.Empty;

                if (basicRows?.Count > 2)
                {
                    intruduce = basicRows?[2].SelectNodes("./span[contains(@class,\"introExpand\")]/text()")?[0].InnerText;

                }

                basedetail.Intruduce = intruduce?.Replace(" ", "").Trim();
                ///////
                HtmlNodeCollection ComInfos = doc.DocumentNode.SelectNodes("//section[@id=\"Cominfo\"]//tr");
                basedetail.LegalRepId = ComInfos?[0].SelectSingleNode("./td/div[contains(@class,\"boss-td\")]//a[@class=\"bname\"]")?.Attributes["href"]?.Value.Replace("/pl/", "").Replace("/firm/", "").Replace(".html", "");
                basedetail.LegalRepName = ComInfos?[0].SelectSingleNode("./td/div[contains(@class,\"boss-td\")]//h2")?.InnerText.Replace(" ", "");
                basedetail.LegalRepImg = ComInfos?[0].SelectSingleNode("./td/div[contains(@class,\"boss-td\")]//img")?.Attributes["src"]?.Value;
                basedetail.RegistrationStatus = ComInfos?[0].SelectNodes("./td")[3]?.InnerText.Replace(" ", "").Trim();
                basedetail.BuildDate = ComInfos?[0].SelectNodes("./td")[5].InnerText.Replace(" ", "").Trim();
                //
                basedetail.Capital = ComInfos?[1].SelectNodes("./td")[1].InnerText.Replace(" ", "").Trim();
                basedetail.IssuedCapital = ComInfos?[1].SelectNodes("./td")[3].InnerText.Replace(" ", "").Trim();
                basedetail.CheckDate = ComInfos?[1].SelectNodes("./td")[5].InnerText.Replace(" ", "").Trim();
                //
                basedetail.SocialCode = ComInfos?[2].SelectNodes("./td")[1].InnerText.Replace(" ", "").Trim();
                basedetail.OrganizationCode = ComInfos?[2].SelectNodes("./td")[3].InnerText.Replace(" ", "").Trim();
                basedetail.RegistrationCode = ComInfos?[2].SelectNodes("./td")[5].InnerText.Replace(" ", "").Trim();
                //
                basedetail.TaxpayerCode = ComInfos?[3].SelectNodes("./td")[1].InnerText.Trim();
                basedetail.ExportCode = ComInfos?[3].SelectNodes("./td")[3].InnerText.Trim();
                basedetail.IndustryCode = ComInfos?[3].SelectNodes("./td")[5].InnerText.Trim();
                //
                basedetail.EnterpriseType = ComInfos?[4].SelectNodes("./td")[1].InnerText.Trim();
                basedetail.OperationEnding = ComInfos?[4].SelectNodes("./td")[3].InnerText.Trim();
                basedetail.Authority = ComInfos?[4].SelectNodes("./td")[5].InnerText.Trim();
                //
                basedetail.StaffSize = ComInfos?[5].SelectNodes("./td")[1].InnerText.Trim();
                basedetail.InsuredSize = ComInfos?[5].SelectNodes("./td")[3].InnerText.Trim();
                basedetail.Area = ComInfos?[5].SelectNodes("./td")[5].InnerText.Trim();
                //
                basedetail.OnceName = ComInfos?[6].SelectNodes("./td")[1].InnerText.Trim();
                basedetail.EnglishName = ComInfos?[6].SelectNodes("./td")[3].InnerText.Trim();
                //
                basedetail.CompanyAddress = ComInfos?[7].SelectNodes("./td")[1].SelectNodes("./span/a")[0]?.InnerText.Trim();
                //
                basedetail.CompanyScope = ComInfos?[8].SelectNodes("./td")[1].InnerText.Trim();

                //////主要人员 - 最新公示/工商登记   
                //mainmember.HTML = doc.DocumentNode.SelectSingleNode("//section[@id=\"Mainmember\"]")?.OuterHtml;

                ////////最终受益人
                //beneficiary.HTML = doc.DocumentNode.SelectSingleNode("//section[@id=\"syrlist\"]")?.OuterHtml;

                //////登记信息
                //registerInfo.HTML = doc.DocumentNode.SelectSingleNode("//section[@id=\"registerInfo\"]")?.OuterHtml;
                
                //////变更历史
                //changelist.HTML = doc.DocumentNode.SelectSingleNode("//section[@id=\"Changelist\"]")?.OuterHtml;

            }
            catch (Exception ex)
            {
                MyLog().Error("基本信息初始化:" + ex.ToString());
            }

        }


    }


    public class boss_search
    {
        public boss_search()
        {
            GroupItems = new List<GroupItem>();
            Paging = new Paging();
            peopledetails = new List<peopledetail>();
         
        }
        public List<GroupItem> GroupItems { get; set; }
        public Paging Paging { get; set; }
        public List<peopledetail> peopledetails { get; set; }
        public int Status { get; set; } = 200;
    }

    public class bigsearch_assets
    {
        public bigsearch_assets()
        {
            GroupItems = new List<GroupItem>();
            Paging = new Paging();
            brands = new List<brand>();
            patents = new List<patent>();
            rjzzqs = new List<rjzzq>();
            zpzzqs = new List<zpzzq>();
        }
        public List<GroupItem> GroupItems { get; set; }
        public Paging Paging { get; set; }
        public List<brand> brands { get; set; }
        public List<patent> patents { get; set; }
        public List<rjzzq> rjzzqs { get; set; }
        public List<zpzzq> zpzzqs { get; set; }
        public int Status { get; set; } = 200;
    }
    public class zpzzq
    {
        public string zpzzqId { get; set; }
        public string zpzzqName { get; set; }
        public string type { get; set; }
        public string code { get; set; }
        public string date1 { get; set; }
        public string date2 { get; set; }
        public string date3 { get; set; }
        public string queryId { get; set; }
        public string queryName { get; set; }
    }

    public class rjzzq
    {
        public string rjzzqId { get; set; }
        public string rjzzqName { get; set; }
        public string rjzzqShortName { get; set; }
        public string code { get; set; }
        public string date { get; set; }
        public string queryId { get; set; }
        public string queryName { get; set; }
    }

    public class brand
    {
        public string brandId { get; set; }
        public string brandName { get; set; }
        public string brandImg { get; set; }
        public string brandtype { get; set; }
        public string Intruduce { get; set; }
        public string code { get; set; }
        public string date { get; set; }
        public string queryId { get; set; }
        public string queryName { get; set; }
        public string status { get; set; }
    }
    public class patent
    {
        public patent()
        {
            TagList = new List<string>();
        }
        public List<string> TagList { get; set; }
        public string patentId { get; set; }
        public string patentName { get; set; }
        public string patentImg { get; set; }
        public string inventor { get; set; }
        public string type { get; set; }
        public string Intruduce { get; set; }
        public string querycode { get; set; }
        public string code { get; set; }
        public string date { get; set; }
        public string postdate { get; set; }
        public string queryId { get; set; }
        public string queryName { get; set; }
    }
    



    public class deadbeatlist
    {
        public deadbeatlist()
        {
            GroupItems = new List<GroupItem>();
            Paging = new Paging();
            Result = new List<peopledetail>();
        }
        public List<GroupItem> GroupItems { get; set; }
        public Paging Paging { get; set; }
        public List<peopledetail> Result { get; set; }
        public int Status { get; set; } = 200;
    }

    public class Paging
    { 
        public int? PageIndex { get; set; }
        public int? PageSize { get; set; }
        public int? TotalRecords { get; set; }

    }
    public class Item
    { 
        public string value { get; set; }
        public int? count { get; set; }
        public string desc { get; set; }
    }
    public class GroupItem
    {
        public string key { get; set; }
        public List<Item> Items { get; set; }
    }
    public class deadbeat
    {
        public string deadbeatid { get; set; }
        public string deadbeatdate { get; set; }
        public string deadbeatcode { get; set; }
        public string deadbeatjudgement { get; set; }
        public string deadbeatresult { get; set; }
        public string type { get; set; }
    }

    public class peopledetail
    {
        public peopledetail()
        {
            TagList = new List<string>();
            deadbeat = new List<deadbeat>();
            hisdeadbeat = new List<deadbeat>();
            relateCompanys = new List<JObject>();
            relatePeoples = new List<JObject>();
        }
        /// <summary>
        /// 标签
        /// </summary>
        public List<string> TagList { get; set; }
        /// <summary>
        /// 法人id
        /// </summary>
        public string LegalRepId { get; set; }
        public string PersonId { get; set; }
        /// <summary>
        /// 法人代表
        /// </summary>
        public string LegalRepName { get; set; }
        /// <summary>
        /// 法人头像
        /// </summary>
        public string LegalRepImg { get; set; }
        /// <summary>
        /// 公司简介
        /// </summary>
        public string Intruduce { get; set; }
        /// <summary>
        /// 身份证
        /// </summary>
        public string code { get; set; }
        /// <summary>
        /// 地区
        /// </summary>
        public string Area { get; set; }
        /// <summary>
        /// 失信记录
        /// </summary>
        public List<deadbeat> deadbeat { get; set; }
        public List<deadbeat> hisdeadbeat { get; set; }
        public List<JObject> relateCompanys { get; set; }
        public List<JObject> relatePeoples { get; set; }

    }

    public class basedetail
    {
        public basedetail()
        {
            TagList = new List<string>();
        }
        public string CompanyId { get; set; }
        /// <summary>
        /// c
        /// </summary>
        public string CompanyName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string CompanyIMG { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string UpdateTime { get; set; }
        /// <summary>
        /// 标签
        /// </summary>
        public List<string> TagList { get; set; }
        /// <summary>
        /// 电话
        /// </summary>
        public string PhoneNum { get; set; }
        /// <summary>
        /// 官网
        /// </summary>
        public string WebSite { get; set; }
        /// <summary>
        /// 邮箱
        /// </summary>
        public string MailBox { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 公司简介
        /// </summary>
        public string Intruduce { get; set; }
        /// <summary>
        /// 法人id
        /// </summary>
        public string LegalRepId { get; set; }
        /// <summary>
        /// 法人代表
        /// </summary>
        public string LegalRepName { get; set; }
        /// <summary>
        /// 法人头像
        /// </summary>
        public string LegalRepImg { get; set; }

        /// <summary>
        /// 登记状态
        /// </summary>
        public string RegistrationStatus { get; set; }
        /// <summary>
        /// 成立日期
        /// </summary>
        public string BuildDate { get; set; }
        /// <summary>
        /// 注册资本
        /// </summary>
        public string Capital { get; set; }
        /// <summary>
        /// 实缴资本
        /// </summary>
        public string IssuedCapital { get; set; }
        /// <summary>
        /// 核准日期
        /// </summary>
        public string CheckDate { get; set; }
        /// <summary>
        /// 统一社会信用代码
        /// </summary>
        public string SocialCode { get; set; }
        /// <summary>
        /// 组织机构代码
        /// </summary>
        public string OrganizationCode { get; set; }
        /// <summary>
        /// 工商注册号
        /// </summary>
        public string RegistrationCode { get; set; }
        /// <summary>
        /// 纳税人识别号
        /// </summary>
        public string TaxpayerCode { get; set; }
        /// <summary>
        /// 进出口企业代码
        /// </summary>
        public string ExportCode { get; set; }
        /// <summary>
        /// 所属行业
        /// </summary>
        public string IndustryCode { get; set; }
        /// <summary>
        /// 企业类型
        /// </summary>
        public string EnterpriseType { get; set; }
        /// <summary>
        /// 营业期限
        /// </summary>
        public string OperationEnding { get; set; }
        /// <summary>
        /// 登记机关
        /// </summary>
        public string Authority { get; set; }
        /// <summary>
        /// 人员规模
        /// </summary>
        public string StaffSize { get; set; }
        /// <summary>
        /// 参保人数
        /// </summary>
        public string InsuredSize { get; set; }
        /// <summary>
        /// 所属地区
        /// </summary>
        public string Area { get; set; }
        /// <summary>
        /// 曾用名
        /// </summary>
        public string OnceName { get; set; }
        /// <summary>
        /// 英文名
        /// </summary>
        public string EnglishName { get; set; }
        /// <summary>
        /// 企业地址
        /// </summary>
        public string CompanyAddress { get; set; }
        /// <summary>
        /// 经营范围
        /// </summary>
        public string CompanyScope { get; set; }

    }
    
    /// <summary>
    /// 主要受益人
    /// </summary>
    public class Beneficiary
    {
        public Beneficiary()
        {
        }
        public string HTML { get; set; }


    }
    /// <summary>
    /// 主要负责人
    /// </summary>
    public class Mainmember
    {
        public Mainmember()
        {
        }
        public string HTML { get; set; }


    }

    /// <summary>
    /// 工商自主公示- 股东及出资信息 
    /// </summary>
    public class Stockholder
    {
        public Stockholder()
        {
        }
        public string HTML { get; set; }
         

    }
    /// <summary>
    /// 工商自主公示- 股权变更信息 
    /// </summary>
    public class StockChange
    {
        public StockChange()
        {
        }
        public string HTML { get; set; }
     
    }

    /// <summary>
    /// 财务分析
    /// </summary>
    public class newFinancialAnalysis
    {
        public newFinancialAnalysis()
        {
        }
        public string HTML { get; set; }

    }
    /// <summary>
    /// 同业分析
    /// </summary>
    public class newIndicatorData
    {
        public newIndicatorData()
        {
        }
        public string HTML { get; set; }

    }
    /// <summary>
    /// 变更记录
    /// </summary>
    public class Changelist
    {
        public Changelist()
        {
        }
        public string HTML { get; set; }

    }

    //
    public class Partnern
    {
        public Partnern()
        {
        }
        public string HTML { get; set; }

    }

    /// <summary>
    /// 等级信息
    /// </summary>
    public class RegisterInfo
    {
        public RegisterInfo()
        {
        }
        public string HTML { get; set; }

    }
    /// <summary>
    /// 全球关联企业
    /// </summary>
    public class Overseaall
    {
        public Overseaall()
        {
        }
        public string HTML { get; set; }

    }
    
}
